// 路由
import router from './router'
// vuex
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()
  // 判断有无token
  if (store.state.user.token) {
    // 在判断跳转到哪里
    if (to.path === '/login') {
      console.log('已经登陆了，就不能再访问login,应该去主页')
      next('/')
    } else {
      // 每次刷新都会执行页面路由的执行 就会执行路由的前置守卫
      await store.dispatch('user/getUserInfo')
      next()
    }
  } else {
    // 没有token 有些页面不需要token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
